<?php
include_once ("../Login/databaseconnection.php");
	$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	$Exam_Name=$_POST['Exam_Name'];
   $Year=$_POST['Year'];
 
  if($SchName!=""&&$Exam_Name!="Select One"&&$Year!="Select One")
	   {
	   $result=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	   	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
$s_name=$row['School_Id'];
$Exam=$row['Exam_Name'];
$Ye=$row['Year'];
$No_Of_Sat=$row['No_Of_Sat_Students'];
$No_Of_Passed=$row['No_Of_Passed_Students'];
}
	   if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   else
   {
     if($_SESSION['Access_Type']=="Administrator")
   {
    
	{
   	  $del=mysql_query("DELETE FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }

  if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
 $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
  }
  }
   } 
     else if($_SESSION['Access_Type']=="Teacher in Charge")
	{	
	$_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   if($Own_School_Id==$School_Id)
   {
   $resulte=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
   $Accessed_By=$_SESSION['NIC_Number'];
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
   if($row=mysql_fetch_array($resulte))
   {
   $School_Id=$row['School_Id'];
   $Exam_Name=$row['Exam_Name'];
   $Year=$row['Year'];
   $No_Of_Sat_Students=$row['No_Of_Sat_Students'];
   $No_Of_Passed_Students=$row['No_Of_Passed_Students'];
   $Last_Updated_Date=$row['Last_Updated_Date'];  
   }
   	 $sql=mysql_query("INSERT INTO deleted_exam(School_Id,Exam_Name,Year,No_Of_Sat_Students,No_Of_Passed_Students,Deleted_date,Accessed_By,Last_Updated_Date) VALUES('$School_Id','$Exam_Name','$Year','$No_Of_Sat_Students','$No_Of_Passed_Students','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql){
		  $del=mysql_query("DELETE FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
  }
 if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_exam WHERE Exam_Name='$Exam_Name' AND year='$Year' AND School_Id='$School_Id' AND Deleted_Date='$t_d'");
  $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
  }
  }
    if(!$sql)
{
 $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
  else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
   $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
  }
  }
   	else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
   $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
  }
  }
  else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
   $s_name='';
$Exam='';
$Ye='';
$No_Of_Sat='';
$No_Of_Passed='';
  }
  }
  }
  }
 if($SchName==""||$Exam_Name=="Select One"||$Year=="Select One")
	   {
		   $_SESSION['errorin']="Please select the School's name,Exam name and Year!";
   	   }  
  ?>